<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

/**
 * 系统菜单管理控制器
 * @package App\Http\Controllers\Admin
 */
class SysMenuController extends BaseController
{
    private $menuSer;

    /**
     * 构造函数
     */
    public function __construct()
    {
        parent::__construct();
        $this->menuSer = new \App\Services\System\Menu();
    }

    /**
     * 菜单管理数据返回
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request)
    {
        //全部分组数据
        $groupList = $this->menuSer->getGroupList();
        //菜单数据
        $groupId = $request->get('group_id', 0);
        //默认分组
        $groupId || $groupId = $groupList ? $groupList->first()->group_id : 0;
        //查询菜单
        $query = \App\Models\System\Menu::query();
        $query->where('group_id', $groupId);
        //搜索菜单名称
        $menuName = $request->input('name');
        $menuName && $query->where('menu_name', 'like', "%".$menuName."%");
        //获取菜单列表
        $menuList = $query->get();
        $menuList = $menuList->isNotEmpty() ? $menuList->toArray() : [];
        if (empty($menuName)){
            $tree = new \App\Services\Tool\TreeService('menu_id', 'parent_id', 'children');
            $tree->load($menuList);
            $treeMenuList = $tree->deepTree();
        }else{
            $treeMenuList = $menuList;
        }
        //权限树结构菜单列表
        $permSer = new \App\Services\System\Permission();
        $permList = $permSer->getCachePermList();
        $tree = new \App\Services\Tool\TreeService('perm_id', 'parent_id', 'children');
        $tree->load($permList);
        $treePermList = $tree->deepTree();
        return $this->success('加载成功', [
            'groupId' => $groupId,
            'groupList' => $groupList,
            'menuList' => $treeMenuList,
            'permList' => $treePermList
        ]);
    }

    /**
     * 保存、添加菜单
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
     */
    public function save(Request $request)
    {
        $this->validate($request,[
            'group_id' => 'required',
            'menu_key' => 'required',
            'menu_name' => 'required',
            //'perm_id' => 'required',
            'icon' => 'required',
            'order_num' => 'integer|min:0',
        ]);
        try {
            $rs = $this->menuSer->saveMenu($request->toArray());
            if ($rs) {
                return $this->success(($request->input('menu_id') ? '编辑' : '添加') . '菜单成功');
            }
            throw new \Exception(($request->input('menu_id') ? '编辑' : '添加') . '菜单失败');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    /**
     * 删除菜单
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
     */
    public function destroy(Request $request)
    {
        try {
            $id = $request->input('menu_ids', []);
            if (!$id) {
                throw new \Exception('请指定要删除的菜单');
            }
            $rs = $this->menuSer->del($id);
            if (!$rs) {
                throw new \Exception('删除失败');
            }
            return $this->success('删除成功');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }
    /**
     * 保存排序数据
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function setOrder(Request $request)
    {
        try {
            $draggingNode = $request->draggingNode ?? [];
            if (empty($draggingNode)){
                throw new \Exception('请指定拖拽节点');
            }
            $dropNode = $request->dropNode ?? [];
            if (empty($dropNode)){
                throw new \Exception('请指定被放置节点');
            }
            $dropType = $request->dropType ?? '';
            if (empty($dropType)){
                throw new \Exception('请指定拖拽类型');
            }
            $rs = $this->menuSer->setOrder($draggingNode, $dropNode, $dropType);
            if ($rs){
                return $this->success('设置排序成功');
            }
            throw new \Exception('设置排序失败');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }
}
